package com.liwc.util;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class BASE64Util {
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		
		String text = "liwenchao李文超";

		String en = encode(text);
		System.out.println(en);
		//解码
		System.out.println(decode(en));

		
	}
	
	/**
	 * BASE64编码
	 * @param text
	 * @return
	 */
	public static String encode(String text){
		final Base64.Encoder encoder = Base64.getEncoder();
		byte[] textByte = null;
		try {
			textByte = text.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		final String encodedText = encoder.encodeToString(textByte);
		return encodedText;
	}
	
	/**
	 * BASE64解码
	 * @param encodedText
	 * @return
	 */
	public static String decode(String encodedText){
		final Base64.Decoder decoder = Base64.getDecoder();
		String text = null;
		try {
			byte[] b = decoder.decode(encodedText);
			text= new String(b, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return text;
	}
	
}
